import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'components/splash_view.dart';
import 'components/love_diary_view.dart';
import 'components/important_days_view.dart';
import 'components/mood_sharing_view.dart';
import 'components/welcome_view.dart';
import 'components/top_back_skip_view.dart';
import 'components/center_next_button.dart';

class IntroductionAnimationScreen extends StatefulWidget {
  const IntroductionAnimationScreen({Key? key}) : super(key: key);

  @override
  _IntroductionAnimationScreenState createState() =>
      _IntroductionAnimationScreenState();
}

class _IntroductionAnimationScreenState
    extends State<IntroductionAnimationScreen> with TickerProviderStateMixin {
  AnimationController? _animationController;

  @override
  void initState() {
    _animationController =
        AnimationController(duration: Duration(seconds: 8), vsync: this);
    _animationController?.animateTo(0.0);
    super.initState();
  }

  @override
  void dispose() {
    _animationController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(_animationController?.value);
    return Scaffold(
      backgroundColor: Color(0xffF7EBF0),
      body: ClipRect(
        child: Stack(
          children: [
            // 背景渐变
            Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color(0xffF7EBF0),
                    Color(0xffFDF2F8),
                  ],
                ),
              ),
            ),
            // 启动页面
            SplashView(
              animationController: _animationController!,
            ),
            // 爱情日记页面
            LoveDiaryView(
              animationController: _animationController!,
            ),
            // 重要日子页面
            ImportantDaysView(
              animationController: _animationController!,
            ),
            // 心情分享页面
            MoodSharingView(
              animationController: _animationController!,
            ),
            // 欢迎页面
            WelcomeView(
              animationController: _animationController!,
            ),
            // 顶部返回跳过按钮
            TopBackSkipView(
              onBackClick: _onBackClick,
              onSkipClick: _onSkipClick,
              animationController: _animationController!,
            ),
            // 中心下一步按钮
            CenterNextButton(
              animationController: _animationController!,
              onNextClick: _onNextClick,
            ),
          ],
        ),
      ),
    );
  }

  void _onSkipClick() {
    _animationController?.animateTo(0.8,
        duration: Duration(milliseconds: 1200));
  }

  void _onBackClick() {
    if (_animationController!.value >= 0 &&
        _animationController!.value <= 0.2) {
      _animationController?.animateTo(0.0);
    } else if (_animationController!.value > 0.2 &&
        _animationController!.value <= 0.4) {
      _animationController?.animateTo(0.0);
    } else if (_animationController!.value > 0.4 &&
        _animationController!.value <= 0.6) {
      _animationController?.animateTo(0.2);
    } else if (_animationController!.value > 0.6 &&
        _animationController!.value <= 0.8) {
      _animationController?.animateTo(0.4);
    } else if (_animationController!.value > 0.8 &&
        _animationController!.value <= 1.0) {
      _animationController?.animateTo(0.6);
    }
  }

  void _onNextClick() {
    if (_animationController!.value >= 0 &&
        _animationController!.value <= 0.2) {
      _animationController?.animateTo(0.4);
    } else if (_animationController!.value > 0.2 &&
        _animationController!.value <= 0.4) {
      _animationController?.animateTo(0.6);
    } else if (_animationController!.value > 0.4 &&
        _animationController!.value <= 0.6) {
      _animationController?.animateTo(0.8);
    } else if (_animationController!.value > 0.6 &&
        _animationController!.value <= 0.8) {
      _animationController?.animateTo(1.0);
    } else if (_animationController!.value > 0.8 &&
        _animationController!.value <= 1.0) {
      // 跳转到主应用
      Navigator.pushReplacementNamed(context, '/main');
    }
  }
}